package com.hnevc.app2;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {
    public static final String TAG = "MainActivity";
    ContentResolver mContentResolver;
    ListView mListView;
    Uri mUri;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContentResolver = getContentResolver();
        mUri = Uri.parse("content://com.hnevc.sqlitedemo/news");
        mListView = (ListView) findViewById(R.id.id_lv_news);

    }

    public void buttonClick(View view){
        switch (view.getId()){
            case R.id.id_btn_add:
            {
                ContentValues values = new ContentValues();
                values.put("title","标题1");
                values.put("author","李白");
                values.put("content","今天的天气真好");
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                values.put("publishDate", sdf.format(Calendar.getInstance().getTime()));
                Uri uri = mContentResolver.insert(mUri,values);
                Log.e(TAG, "bttonClick: "+uri.toString() );
                break;
            }
            case R.id.id_btn_del:
            {
                mContentResolver.delete(mUri,"author=?",new String[]{"李白"} );
                break;
            }
            case R.id.id_btn_query:
            {
                //查询
                Cursor cursor = mContentResolver.query(mUri,null,null,null,null);
                CursorAdapter adapter = new SimpleCursorAdapter(
                        this,
                        R.layout.list_item,
                        cursor,
                        new  String[]{"author","content"},
                        new int[]{R.id.id_tv_author,R.id.id_tv_content}
                );
                mListView.setAdapter(adapter);
                break;
            }
            case R.id.id_btn_update:
            {
                ContentValues values = new ContentValues();
                values.put("author","李白白");

                mContentResolver.update(mUri,values,"author=?",new String[]{"李白"});
                break;
            }
        }
    }
}
